<!--
 * @Description: 减脂营新增表单
 * @Version: 1.0
 * @Author: Yuanjy
 * @Date: 2020-03-31 12:04:35
 * @LastEditors: Yuanjy
 * @LastEditTime: 2020-05-12 17:36:15
 -->
<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增减脂营' : '编辑减脂营'" width="500px">
    <el-form ref="form" :model="form" :rules="formRules" size="small" label-width="100px">
      <el-form-item label="减脂营名称" prop="camp_name">
        <el-input placeholder="减脂营名称" style="width:80%" v-model="form.camp_name" />
      </el-form-item>
      <el-form-item label="渠道名称">
        <treeselect v-model="form.channel_code" :options="secondChannels" :flatten-search-results="true" :disable-branch-nodes="true" :show-count="true" style="width:80%" placeholder="请选择渠道" />
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialog = false">取 消</el-button>
      <el-button type="primary" @click="doSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    secondChannels: {
      type: Array,
      required: true
    },
    supThis: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      loading: false, 
      dialog: false,
      form: {
        id: '',
        camp_name: '',
        channel_code: null,
      },
      formRules: {
        camp_name: [
          { required: true, message: '请输入减脂营名称', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else this.doEdit()
        } else {
          console.log('error')
          return false
        }
      })
    },
    doAdd() {
      this.$api.order.camp
        .add({
          data: this.form
        })
        .then(res => {
          this.resetForm()
          this.$notify({ title: '添加成功', type: 'success', duration: 2500 })
          this.loading = false
          this.$parent.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    doEdit() {
      this.$api.order.camp
        .edit({
          data: this.form
        })
        .then(res => {
          this.resetForm()
          this.$notify({ title: '修改成功', type: 'success', duration: 2500 })
          this.loading = false
          this.supThis.init()
        }).catch(err => {
          this.loading = false
          console.log(err)
        })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        delivery_name: '',
        channel_code: '',
        phone: '',
        order_type: '',
        goods_info: '',
      }
    }
  }
}
</script>

<style scoped>

</style>
